<template>
  <el-dialog v-model="dialogVisible" title="修改密码" width="500px" draggable>
    <el-form ref="formRef" :inline="true" :model="formData" :rules="rules" class="demo-form-inline" label-width="5rem">
      <el-row :gutter="20">
        <el-col :span="24">
          <el-form-item label="电话" prop="phone">
            <el-input v-model="formData.phone" clearable disabled placeholder="" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="旧密码" prop="oldPassword">
            <el-input v-model="formData.oldPassword" clearable placeholder="" show-password type="password" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="新密码" prop="newPassword">
            <el-input v-model="formData.newPassword" clearable placeholder="" show-password type="password" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="handleConfirm">确认</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { computed, reactive, ref } from "vue";
import { useUserStore } from "@/stores/modules/user";
import { updateUserPassword } from "@/api/modules/user";
import md5 from "md5";
import { ElLoading, ElMessage } from "element-plus";

const dialogVisible = ref(false);
const formRef = ref();
const userStore = useUserStore();
const formData = reactive({
  phone: computed(() => userStore.userInfo.phone),
  oldPassword: "",
  newPassword: ""
});
const rules = reactive({
  phone: [{ required: true, message: "请输入电话", trigger: "blur" }],
  oldPassword: [{ required: true, message: "请输入旧密码", trigger: "blur" }],
  newPassword: [
    { required: true, message: "请输入新密码", trigger: "blur" },
    { min: 6, message: "密码长度至少为6位", trigger: "blur" }
  ]
});

const openDialog = () => {
  dialogVisible.value = true;
};

const handleConfirm = () => {
  formRef.value?.validate((valid: boolean) => {
    if (valid) {
      // 显示加载状态
      ElLoading.service({
        lock: true,
        text: "正在修改密码...",
        background: "rgba(0, 0, 0, 0.1)"
      });

      updateUserPassword({
        oldPassword: md5(formData.oldPassword),
        newPassword: md5(formData.newPassword)
      })
        .then(res => {
          // 成功提示
          ElMessage({
            type: "success",
            message: "密码修改成功，请使用新密码登录！",
            duration: 3000
          });
        })
        .catch(err => {
          // 失败提示
          ElMessage({
            type: "error",
            message: err.message || "密码修改失败，请重试！",
            duration: 3000
          });
        })
        .finally(() => {
          // 关闭加载状态
          ElLoading.service().close();
          dialogVisible.value = false;
        });
    } else {
      // 校验失败提示
      ElMessage({
        type: "warning",
        message: "请完成必填项并检查表单错误！",
        duration: 3000
      });
      console.log("表单数据校验失败");
      return false;
    }
  });
};

defineExpose({ openDialog });
</script>
